﻿using BootyBay.PrivateStoreApp.DataModel;
using BootyBay.PrivateStoreApp.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “用户控件”项模板在 http://go.microsoft.com/fwlink/?LinkId=234236 上提供

namespace BootyBay.PrivateStoreApp.Controls
{
    public sealed partial class MainPageFlipViewControl : UserControl
    {
        private DispatcherTimer timer = new DispatcherTimer();//计时器，每 5s FlipView滚动一次
        public MainPageFlipViewControl()
        {
            this.InitializeComponent();
            this.DataContext = AppListPageVM.Instance;
            LoadInfo();
        }

        #region FlipView内容定时滚动
        private void LoadInfo()
        {
            timer.Interval = new TimeSpan(0, 0, 5);
            timer.Tick -= timer_Tick;
            timer.Tick += timer_Tick;
            timer.Start();
        }

        void timer_Tick(object sender, object e)
        {
            if (itemFlipView.Items.Count > 0)
            {
                if (itemFlipView.SelectedIndex + 1 >= itemFlipView.Items.Count)
                {
                    itemFlipView.SelectedIndex = 0;
                }
                else
                {
                    itemFlipView.SelectedIndex++;
                }
            }
        }
        #endregion

        #region 单击FlipViewItem时跳转到应用滚动详细页面
        private void itemFlipView_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            UserAppDataModel items = (UserAppDataModel)itemFlipView.SelectedItem;
            Frame rootFrame = Window.Current.Content as Frame;

            if (items.AppView.IsPublicApp)
            {
                if (items.AppView.PublisherDisplayName != "")
                {
                    AppListPageVM.Instance.OpenPublicAppDetailPage(items.AppView.Publisher);
                }
            }
            else
            {
                rootFrame.Navigate(typeof(ItemDetailPage), items);
            }
        }
        #endregion

        #region 鼠标移入停止滚动
        private void itemFlipView_PointerEntered(object sender, PointerRoutedEventArgs e)
        {
            timer.Stop();
        }
        #endregion

        #region 鼠标移出开始滚动
        private void itemFlipView_PointerExited(object sender, PointerRoutedEventArgs e)
        {
            timer.Stop();
            timer.Start();
        }
        #endregion


    }
}
